package RabbitMQ.Publisher.Confirms;

import RabbitMQ.Constants.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeoutException;

public class PublisherConfirms {
    public static final int MESSAGE_COUNT=200;
    static Connection createConnection() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL_HOST);
        Connection connection=connectionFactory.newConnection();
        return connection;
    }
    public static void main(String[] args) throws Exception{
//        publishingMessagesIndividually
//        单独确认
        publishingMessagesIndividually();
        publishingMessagesInBatches();
        handlingPublisherConfirmAsychronously();

    }
//    这里写三个方法,
//    单独确认
    private static void publishingMessagesIndividually() throws IOException, TimeoutException, InterruptedException {
        Connection connection=createConnection();
//      1.  开启信道
        Channel channel=connection.createChannel();
//      2.设置信道为confirm模式
        channel.confirmSelect();
//       3.声明队列
        channel.queueDeclare(Constants.PUBLISHER_CONFIRMS_QUEUE1,true,false,false,null);
//        4.发送消息
//        这里发送两百条消息,在开始和结束的时候即使,比较一下三种的效率
        long start=System.currentTimeMillis();
//        因为是单独确认,所以就是发布一条消息,就要确认一条
        for(int i=0;i<MESSAGE_COUNT;i++){
            String msg="hello publisher confirms"+i;
            channel.basicPublish("",Constants.PUBLISHER_CONFIRMS_QUEUE1,null,msg.getBytes(StandardCharsets.UTF_8));
//               这里将等待时间设置为5s
            channel.waitForConfirmsOrDie(5000);
        }
        long end=System.currentTimeMillis();
        System.out.printf("单独确认策略,消息条数: %d,耗时: %d ms\n",MESSAGE_COUNT,end-start);
    }
//    批量确认
    private static void publishingMessagesInBatches() throws IOException, TimeoutException, InterruptedException {
//        批量确认就是返回一个确认序号,以确定确认序号之前的消息都收到了
        Connection connection=createConnection();
        Channel channel=connection.createChannel();
//        设置信道为confirm模式
        channel.confirmSelect();
//        声明队列
        channel.queueDeclare(Constants.PUBLISHER_CONFIRMS_QUEUE2,true,false,false,null);
        Long start=System.currentTimeMillis();
//        发送消息,并进行确认.每一百条消息进行一次确认
        int batchSize=100;
//        这个变量用来记录已经发送的消息条数
        int outstandingMessageCount=0;
        for(int i=0;i<MESSAGE_COUNT;i++){
            String msg="hello publlisher confirms"+i;
//            发送消息
            channel.basicPublish("",Constants.PUBLISHER_CONFIRMS_QUEUE2,null,msg.getBytes(StandardCharsets.UTF_8));
            outstandingMessageCount++;
            if(outstandingMessageCount==batchSize){
                channel.waitForConfirmsOrDie(5000);
                outstandingMessageCount=0;
            }

        }
        if(outstandingMessageCount>0){
            channel.waitForConfirmsOrDie(5000);
        }
        long end=System.currentTimeMillis();
        System.out.printf("批量确认策略,消息条数: %d,耗时: %d ms\n",MESSAGE_COUNT,end-start);
    }
//    异步确认
    private static void handlingPublisherConfirmAsychronously(){

    }
//    由于都要进行建立连接这个操作,所以可以把这个公用的代码,写成一个静态内部类




}
